package cz.muni.fi;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class StewardDAO implements StewardDAOLocal {

    @PersistenceContext
    private EntityManager em;

    @Override
    public Steward get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        Steward steward;

        try {
            steward = em.find(Steward.class, id);
            return steward;
        } catch (Exception ex) {
            throw new ServiceFailureException(ex);
        }
    }

    @Override
    public void create(Steward steward) {
        if (steward == null) {
            throw new IllegalArgumentException("steward is null");
        }
        if (steward.getId() != null) {
            throw new IllegalArgumentException("steward has id");
        }

        try {
            em.persist(steward);
        } catch (Exception ex) {
            throw new ServiceFailureException(ex);
        }
    }

    @Override
    public void update(Steward steward) {
        if (steward == null) {
            throw new IllegalArgumentException("steward is null");
        }
        if (steward.getId() == null) {
            throw new IllegalArgumentException("steward has no id");
        }

        try {
            em.merge(steward);
        } catch (Exception ex) {
            throw new ServiceFailureException(ex);
        }
    }

    @Override
    public void remove(Steward steward) {
        if (steward == null) {
            throw new IllegalArgumentException("steward is null");
        }
        if (steward.getId() == null) {
            throw new IllegalArgumentException("steward has no id");
        }

        try {
            em.remove(em.find(Steward.class, steward));
        } catch (Exception ex) {
            throw new ServiceFailureException(ex);
        }
    }

    @Override
    public void refresh(Steward steward) {
        if (steward == null) {
            throw new IllegalArgumentException("steward is null");
        }
        if (steward.getId() == null) {
            throw new IllegalArgumentException("steward has no id");
        }

        try {
            em.refresh(steward);
        } catch (Exception ex) {
            throw new ServiceFailureException(ex);
        }
    }

    @Override
    public List<Steward> findAll() {
        List<Steward> stewards;

        try {
            stewards = em.createQuery("Select p from Steward p").getResultList();
            return stewards;
        } catch (Exception ex) {
            throw new ServiceFailureException(ex);
        }
    }

    @Override
    public Steward findByPersonalId(Long personalId) {
        if (personalId == null) {
            throw new IllegalArgumentException("personalId is null");
        }
        Steward steward;

        try {
            Query q = em.createQuery("Select s from Steward s where personalId = :personalId").setParameter("personalId", personalId);
            steward = (Steward) q.getSingleResult();
            return steward;
        } catch (Exception ex) {
            throw new ServiceFailureException(ex);
        }
    }

    @Override
    public List<Steward> findByAnyName(String anyName) {
        if (anyName == null) {
            throw new IllegalArgumentException("anyName is null");
        }

        List<Steward> stewards;

        try {
            stewards = em.createQuery("SELECT s FROM Steward AS s WHERE s.firstName = :firstName OR s.lastName = :lastName").setParameter("firstName", anyName).setParameter("lastName", anyName).getResultList();
            return stewards;
        } catch(Exception ex){
            throw new ServiceFailureException(ex);
        }
    }
}